package com.example.luke.cashorderingsystem.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.luke.cashorderingsystem.Model.Good;
import com.example.luke.cashorderingsystem.Model.PurchaseReport;
import com.example.luke.cashorderingsystem.R;

import java.util.ArrayList;
import java.util.Locale;

/**
 * Created by luke on 21/11/2016.
 */

public class PurchaseReportAdapter extends BaseAdapter {
    Context context;
    LayoutInflater inflater;
    ArrayList<PurchaseReport>purchaseReportArrayList;

    public PurchaseReportAdapter(Context context){
        inflater=LayoutInflater.from(context);
        purchaseReportArrayList=new ArrayList<>();


    }

    public void setPurchaseReportArrayList(ArrayList<PurchaseReport> purchaseReportArrayList) {
        this.purchaseReportArrayList = purchaseReportArrayList;
        notifyDataSetChanged();
    }

    public ArrayList<PurchaseReport> getPurchaseReportArrayList() {

        return purchaseReportArrayList;
    }


    @Override
    public int getCount() {
        return purchaseReportArrayList.size();

    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public PurchaseReport getItem(int position) {
        return purchaseReportArrayList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       if (convertView==null){

           convertView=inflater.inflate(R.layout.purchase_report_row,null);

       }
        ImageView tick=(ImageView)convertView.findViewById(R.id.tick);
        TextView no=(TextView)convertView.findViewById(R.id.no);

        TextView ticketID=(TextView)convertView.findViewById(R.id.ticketID);

        TextView commodityID=(TextView)convertView.findViewById(R.id.commodityID);
        TextView commodityName=(TextView)convertView.findViewById(R.id.commodityName);

        TextView volume=(TextView)convertView.findViewById(R.id.volume);
        TextView primeCost=(TextView)convertView.findViewById(R.id.primeCost);
        TextView saleCost=(TextView)convertView.findViewById(R.id.saleCost);
        TextView cost=(TextView)convertView.findViewById(R.id.cost);
        TextView restoredNum=(TextView)convertView.findViewById(R.id.restoredNum);
        TextView discount=(TextView)convertView.findViewById(R.id.discountRate);
        TextView dateTime=(TextView)convertView.findViewById(R.id.dateTime);
        TextView employeeID=(TextView)convertView.findViewById(R.id.employeeID);

       PurchaseReport report=getItem(position);

        if (report.getUploadMark()==1)
            tick.setVisibility(View.VISIBLE);
        else
         tick.setVisibility(View.GONE);

        no.setText(String.valueOf(position+1));
        ticketID.setText(getItem(position).getTicketID());
        commodityID.setText(getItem(position).getCommodityID());
        commodityName.setText(getItem(position).getCommodityName());
        volume.setText(report.getVolume()+"");

       primeCost.setText( String.format(Locale.getDefault(),"%.2f",Double.valueOf(report.getPrimeCost()))+"元");
        saleCost.setText( String.format(Locale.getDefault(),"%.2f",Double.valueOf(report.getSaleCost()))+"元");

       cost.setText(report.getCost()+"元");
       restoredNum.setText(report.getRestoredNum()+"");
       discount.setText(report.getDisCount());
        dateTime.setText(report.getDateTime());
       employeeID.setText(report.getEmployeeID());

        return convertView;
    }
}
